package com.service.purchase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.common.spring.SpringMvcUtils;
import com.dao.purchase.PlanMapper;
import com.dao.purchase.PlanMmMapper;
import com.dao.purchase.ProgramMapper;
import com.dao.purchase.ProgramMmMapper;
import com.model.purchase.PurchaseInquiry;
import com.model.purchase.PurchaseNeed;
import com.model.purchase.PurchasePlan;
import com.model.purchase.PurchasePlanMm;
import com.model.purchase.PurchasePlanVO;
import com.model.purchase.PurchaseProgram;
import com.model.purchase.PurchaseProgramMm;
import com.model.sys.SysUser;

@Service
public class PlanService {

	static final Logger logger = LoggerFactory.getLogger(PlanService.class);

	@Autowired
	PlanMapper planMapper;
	@Autowired
	PlanMmMapper planMmMapper;
	@Autowired
	ProgramMapper programMapper;
	@Autowired
	ProgramMmMapper programMmMapper;

	/**
	 * 计划列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectPlanList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<PurchasePlan> list = planMapper.selectList(queryMap);
		int count = planMapper.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectPlanList:{}", list);

		return httpResult;
	}

	/**
	 * 计划明细列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectPlanMmList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
			if (StringUtils.isNotNull(sysUser.getDatapermission())) {
				queryMap.put("deptIds", sysUser.getDatapermission().split(","));
			} else {
				queryMap.put("createId", sysUser.getUserid());
			}
		}
		List<PurchasePlanMm> list = planMmMapper.selectList(queryMap);
		int count = planMmMapper.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectPlanMmList:{}", list);

		return httpResult;
	}

	/**
	 * 保存采购计划
	 * 
	 * @param purchasePlanMm
	 * @return
	 */
	@Transactional
	public HttpResult savePlan(PurchasePlanVO purchasePlanVO) {
		if (purchasePlanVO.getPurchasePlanMms() == null) {
			return HttpResult.error("参数不能为空");
		}

		List<PurchasePlanMm> purchasePlanMms = purchasePlanVO.getPurchasePlanMms();
		int result = 0;
		for (PurchasePlanMm purchasePlanMm : purchasePlanMms) {
			purchasePlanMm.setStatus(purchasePlanVO.getStatus());
			result = planMmMapper.update(purchasePlanMm);
		}
		HttpResult httpResult = new HttpResult();

		if (result > 0) {
			httpResult.setData(purchasePlanVO);
			httpResult.setMsg(purchasePlanVO.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			return httpResult;
		} else {
			return HttpResult.error(purchasePlanVO.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
	}

	/**
	 * 回退
	 * 
	 * @param purchasePlanMm
	 * @return
	 */
	@Transactional
	public HttpResult backPlan(String planId) {
		if (!StringUtils.isNotNull(planId)) {
			return HttpResult.error("参数不能为空!");
		}
		// 明细表改为非提交状态
		PurchasePlanMm planMm = planMmMapper.load(planId);
		if (planMm.getStatus().equals(Global.NUMERICAL_TWO + "")) {
			return HttpResult.error("该物料已生成采购方案，不可回退");
		}
		planMm.setStatus(Global.NO + "");
		int result = planMmMapper.update(planMm);
		if (result > 0) {
			return HttpResult.ok("回退成功");
		} else {
			return HttpResult.error("不存在此采购计划");
		}
	}

	/**
	 * 提交
	 * 
	 * @param purchasePlanMm
	 * @return
	 */
	@Transactional
	public HttpResult commitPlan(PurchasePlanVO purchasePlanVO) {
		if (purchasePlanVO.getPurchasePlanMms() == null) {
			return HttpResult.error("方案参数不能为空");
		}
		int result = 0;
		List<PurchasePlanMm> purchasePlanMms = purchasePlanVO.getPurchasePlanMms();
		for (PurchasePlanMm purchasePlanMm : purchasePlanMms) {
			purchasePlanMm.setStatus(Global.YES + "");
			result += planMmMapper.update(purchasePlanMm);
		}
		if (result > 0) {
			return HttpResult.ok("提交成功");
		} else {
			return HttpResult.error("提交失败");
		}
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void exportPlan(Map<String, Object> queryMap, HttpServletResponse response) {
		List<PurchasePlanMm> list = planMmMapper.selectList(queryMap);

		logger.info("exportPlan:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), PurchasePlanMm.class).sheet("采购计划").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
